package com.gai.shop.service.impl;

import com.gai.shop.common.Const;
import com.gai.shop.dao.CartDao;
import com.gai.shop.dao.ItemDao;
import com.gai.shop.dao.OrdersDao;
import com.gai.shop.dao.impl.CartDaoImpl;
import com.gai.shop.dao.impl.ItemDaoImpl;
import com.gai.shop.dao.impl.OrdersDaoImpl;
import com.gai.shop.entity.Cart;
import com.gai.shop.entity.Item;
import com.gai.shop.entity.Orders;
import com.gai.shop.service.OrdersService;
import com.gai.shop.util.RandomUtils;
import com.gai.shop.util.StrUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    private final OrdersDao OD = new OrdersDaoImpl();
    private final ItemDao ID = new ItemDaoImpl();

    /**
     * 订单的创建
     * @param aid
     * @param uid
     */
    @Override
    public void InsertIntoOrdersByAidAndUid(Integer aid, Integer uid) {
        try {
            //1.获取uid和aid
            if (aid>0) {
                //2.创建orders对象 用于保存订单信息给数据库去
                Orders orders = new Orders();
                //数据封装
                //3.使用RandomUtils工具类生成订单编号，方便我们后面的Item数据转存
                orders.setUid(uid);
                orders.setAid(aid);
                orders.setOtime(new Date());//保存当前的时间
                orders.setOstate(0);//新创建的订单肯定默认为未支付
                String orderId = RandomUtils.createOrderId();
                orders.setOid(orderId);
                //4.通过uid来查找购物车的所有数据信息
                CartDao cd = new CartDaoImpl();
                List<Cart> cartList = cd.selectCartProductByUidAndPid(uid);
                //5.Cart转存数据到item，同时计算总金额 循环操作item保存oid值
                Double ocount = 0.0;
                List<Item> itemList = new ArrayList<Item>();
                for (Cart cart : cartList) {
                    Item item = new Item();
                    item.setInum(cart.getCnum());
                    item.setPid(cart.getProduct().getPid());
                    item.setIcount(cart.getCcount());
                    ocount += cart.getCcount();//计算我们订单的总金额
                    item.setOid(orderId);
                    itemList.add(item);
                }
                orders.setOcount(ocount);//设置总金额
                //6.去数据库保存orders，在List<Item> 批量新增操作
                OD.insertIntoOrders(orders);//订单新增
                ID.insertIntoItems(itemList);//传入做批量新增
                //7.清空购物车数据
                cd.deleteCartByUid(uid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用uid获取用户名下所有的订单信息
     * @param uid
     * @return
     */
    @Override
    public List<Orders> selectOrdersAllByUid(Integer uid) {
        List<Orders> ordersList = null;
        try {
            if (uid>0) {
                ordersList = OD.selectOrdersAllByUid(uid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ordersList;
    }

    /**
     * 使用oid查询item集合
     * @param oid
     * @return
     */
    @Override
    public List<Item> getItemByOid(String oid) {
        List<Item> itemList = null;
        try {
            if (StrUtils.empty(oid)){
                itemList = ID.getItemByOid(oid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return itemList;
    }

    /**
     * 使用oid查询Orders数据
     * @param oid
     * @return
     */
    @Override
    public Orders getOrdersByOid(String oid) {
        Orders orders = null;
        try {
            if (StrUtils.empty(oid)){
                orders = OD.getOrdersByOid(oid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orders;
    }

    /**
     * 支付成功
     * @param oid
     * @return
     */
    @Override
    public Integer updateOrderStateToPaySuccess(String oid) {
        Integer result = 0;
        try {
            if (StrUtils.empty(oid)){
                result = OD.updateOrderStateToPaySuccess(oid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取所有用户的订单数据 动态查询
     * @param uname
     * @param ostate
     * @return
     */
    @Override
    public List<Orders> getAllOrders(String uname, String ostate) {
        List<Orders> ordersList = null;
        try {
            ordersList = OD.getAllOrders(uname, ostate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ordersList;
    }

    /**
     *
     * @param oid
     * @param ostate
     * @return
     */
    @Override
    public Integer updateOrderStateToSend(String oid, Integer ostate) {
        Integer result = 0;
        try {
            Orders order = OD.getOrdersByOid(oid);
            Integer ostates = order.getOstate();
            if (ostates == Const.OrderConst.ORDER_PAY_SUCCESS) {//只有处于已付款的状态c才可以修改为发货
                result = OD.updateOrderStateToSend(oid, ostate);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
